<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm">
			<u-form-item label-width="350" :label-position="labelPosition" label="外径(mm)" prop="outerDiameter">
				<u-input :border="border" placeholder="请输入外径" v-model="model.outerDiameter" type="digit"></u-input>
			</u-form-item>
			<u-form-item label-width="350" :label-position="labelPosition" label="内径(mm)" prop="innerDiameter">
				<u-input :border="border" placeholder="请输入内径" v-model="model.innerDiameter" type="digit"></u-input>
			</u-form-item>
			<u-form-item label-width="350" :label-position="labelPosition" label="压力(KN)" prop="torque">
				<u-input :border="border" placeholder="请输入扭矩" v-model="model.torque" type="digit"></u-input>
			</u-form-item>
		</u-form>

		<u-button type="primary" @click="submit">计算</u-button>

		<view class="u-config-wrap">
			<view class="u-config-item">
				<view class="u-item-title">计算结果</view>
				<u-form-item style="font-size: 22px;" :leftIconStyle="{color: '#888', fontSize: '32rpx'}"
					label-width="300" :label-position="labelPosition" label="压缩应力" prop="torsionalStress">
					<span v-if="model.torsionalStress"> {{model.torsionalStress}} MPa </span>
				</u-form-item>
			</view>
			<view class="u-config-item">
				<view class="u-item-title">计算公式</view>
				<view class="u-item-desc">压缩应力 = 压力 / 截面积</view>
				<view class="u-item-desc">其中：截面积 = (外径^2-内径^2)*0.7854；

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				model: {
					outerDiameter: '',
					innerDiameter: '',
					torque: '',
					torsionalStress: ''
				},
				rules: {
					outerDiameter: [{
						required: true,
						message: '请输入外径',
						trigger: 'blur'
					}],
					innerDiameter: [{
						required: true,
						message: '请输入内径',
						trigger: 'blur'
					}],
					torque: [{
						required: true,
						message: '请输入扭矩',
						trigger: 'blur'
					}]
				},
				labelPosition: 'left',
				border: true,

			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						const outerD = this.model.outerDiameter
						const innerD = this.model.innerDiameter
						const torque = parseFloat(this.model.torque);

						// 扭转应力 = 扭矩 × 1600 × 外径 ÷ π ÷ (外径⁴ - 内径⁴)

						const denominator = (outerD * outerD - innerD * innerD) * 0.7854
						this.model.torsionalStress = ((torque / denominator) * 1000).toFixed(2);
					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.u-config-wrap {
		margin-top: 30rpx;
	}

	.u-config-title {
		padding: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.u-config-item {
		padding: 20rpx;
	}

	.u-item-title {
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.u-item-desc {
		font-size: 24rpx;
		color: #666;
	}
</style>